<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\AvatarVerify;
use App\Admin\Extensions\MomentOp;
use App\Http\Controllers\Controller;
use App\Model\Guild\GuildHostManage;
use App\Model\GuildManage;
use App\Model\Message;
use App\Model\Moment;
use App\Model\UserActLog;
use App\Model\UserAvatarLog;
use App\Model\UserProfile;
use App\Util\AliyunUtil;
use App\Util\ComUtil;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class UserAvatarController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('普通用户头像审核');
            $content->description('');
            $content->body($this->user_grid());
        });
    }

    /**
     * 女用户头像
     */
    public function host_avatar()
    {
        return Admin::content(function (Content $content) {
            $content->header('女用户头像审核');
            $content->description('');
            $content->body($this->host_grid());
        });
    }

    protected function user_grid()
    {
        return Admin::grid(UserAvatarLog::class, function (Grid $grid) {
            $where = [
                'role' => UserProfile::ROLE_USER
            ];
//            $where = [
//                'act' => UserActLog::ACT_AVATAR,
//            ];
//            $hostIds = UserProfile::getHostIds();
            $grid->model()->where($where)->orderBy('id', 'desc');
            return $this->buildGrid($grid);
        });
    }

    protected function host_grid()
    {
        return Admin::grid(UserAvatarLog::class, function (Grid $grid) {
            $where = [
                'role' => UserProfile::ROLE_HOST
            ];
            $adminId = Admin::user()->id;
            if($adminId == 51){
                $ghId= GuildManage::getGuidIdByGuildUserId($adminId);

                $userIds = GuildHostManage::whereIn('guild_manage_id',$ghId)->pluck('host_user_id')->toArray();

//            $where = [
//                'act' => UserActLog::ACT_AVATAR,
//            ];
//            $hostIds = UserProfile::getHostIds();
                $grid->model()->where($where)->whereIn('user_id',$userIds)->orderBy('id', 'desc');
            }else{
                $grid->model()->where($where)->orderBy('id', 'desc');
            }

            return $this->buildGrid($grid);
        });
    }

    /**
     * 表格
     */
    function buildGrid($grid)
    {
        $grid->id('记录ID');
        $grid->userProfile()->user_profile_id('用户ID');
        $grid->userProfile()->gender('性别')->display(function ($gender) {
            return $gender == 1 ? '男' : ($gender == 0 ? '女' : '-');
        });
        $grid->userProfile()->nick_name('用户昵称');
        $grid->userProfile()->role('用户角色')->display(function ($data) {
            return UserProfile::ROLE_HOST == $data ? '女用户' : '普通用户';
        });
        $grid->detail('头像')->display(function ($data) {
            if (!$data) return '';
            return ComUtil::buidlImgaeHtml($data);
        });
        $grid->personalProfile()->face_vry_pic('实名认证头像')->display(function ($data) {
            if (!$data) return '';
            return ComUtil::buidlImgaeHtml($data);
        });
        $grid->avatarVerf()->content('真人头像认证')->display(function ($data) {
            if (!$data) return '-';
            if (!isset($data['real_avatar'])) return '-';
            $data = json_decode($data, true);
            return ComUtil::buidlImgaeHtml($data['real_avatar']);
        });

        $grid->created_at('更新时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->flag('状态')->display(function ($flag) {
            return UserAvatarLog::FLAG[$flag];
        });

        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('userProfile.user_profile_id', '用户ID');
            });
            // $filter->column(1 / 3, function ($filter) {
            //     $filter->equal('userProfile.role', '角色')->select([UserProfile::ROLE_USER => '普通用户', UserProfile::ROLE_HOST => '女用户']);
            // });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('flag', '状态')->select(UserAvatarLog::FLAG);
            });
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->append(new AvatarVerify($actions->getKey(), $actions->row, 'avatar/verify'));
        });
        return $grid;
    }

    /**
     * 审核头像
     */
    function verify()
    {
        $id = request('id');
        $status = intval(request('status'));
        $record = UserAvatarLog::find($id);
        if (!$record) return ['ret' => -1, 'message' => '记录不存在'];
        //修改状态
        $record->update(['flag' => $status]);
        //违规操作
        $role = intval(request('extra'));
        if (UserAvatarLog::FLAG_OK == $status) {
            UserProfile::where('user_id', $record->user_id)->limit(1)->update(['avatar' => $record->detail]);
        } elseif (UserAvatarLog::FLAG_BAD == $status) {
            UserProfile::setDefaultAvatar($record->user_id);

            // 头像违规，把真人认证修改未认证
            UserProfile::where('user_id', $record->user_id)->limit(1)->update(['avatar_vrfy' => 0]);

            // 真人认证不通过后，女生角色修改
            $userProfile = UserProfile::getProfile($record->user_id);
            if ($userProfile->gender == 0) {
                UserProfile::where('user_id', $record->user_id)->limit(1)->update(['role' => UserProfile::ROLE_USER]);
            }

            //站内信
            $systemMsg = "亲爱的用户：您于 {$record->created_at} 上传的头像未达到平台的审核标准，已被系统删除。请参考以下建议重新上传，谢谢！
        1.清晰、正面、单人、大头照是最佳选择；
        2.不要让头发、墨镜遮挡住您自信的脸庞，露出完整脸庞的正面为宜；
        3.请勿上传宠物、卡通人物、明星名人等非本人头像；
        4.头像不能含有暴力、色情、广告、联系方式等违规信息";
            Message::pushSystem($record->user_id, $systemMsg);
        }
        return ['ret' => 0, 'message' => '操作成功！'];
    }
}
